package com.jd;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.selenium.PhantomjsDownloader;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.TargetUrl;

@TargetUrl("https://search.jd.com/Search\\?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8")
@ExtractBy(value = "//li[@class=\"gl-item\"]", multi = true)
public class Jd {

    @ExtractBy("//div[@class=\"p-name p-name-type-2\"]/a/text()")
    private String title;

    @ExtractBy("//div[@class=\"p-price\"]//i/text()")
    private String price;

    public String getTitle() {
        return title;
    }

    public String getPrice() {
        return price;
    }

    public static void main(String[] args) {
        // OOSpider.create(Site.me(), new ConsolePageModelPipeline(), Jd.class)
        // .addUrl("https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8")
        // .setDownloader(new PhantomjsDownloader()).start();
        Page page = new PhantomjsDownloader()
                .download(new Request("https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8"), new Task() {

                    @Override
                    public String getUUID() {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public Site getSite() {
                        return Site.me();
                    }
                });
        System.out.println(page.getHtml());
    }

}
